package cn.edu.pzhu.javaweb.controller;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import cn.edu.pzhu.javaweb.pojo.FileMsg;
import cn.edu.pzhu.javaweb.pojo.Result;
import cn.edu.pzhu.javaweb.service.FileService;
import cn.edu.pzhu.javaweb.service.imp.FileServiceImp;

/**
 * Servlet implementation class ShowFilesServlet
 */
@WebServlet("/showfiles")
public class ShowFilesServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private  FileService  fileservice = new FileServiceImp();   
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ShowFilesServlet() {
    	
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
		throws ServletException, IOException {
        Result<List<FileMsg>> result = fileservice.showFilesList();
        int page=1,num=10,totalpage=0;
        String str_page = request.getParameter("page");
        try {
        	page = Integer.parseInt(str_page);
        }catch(Exception e) {
        	page = 1;
        }
        String str_num = request.getParameter("num");
        if(str_num==null) {
        	Object obj = request.getSession().getAttribute("num");
        	if(obj instanceof String str) {
        		str_num = str;
        	}
        }
        try {
        	num = Integer.parseInt(str_num);
        }catch(Exception e) {
        	num = 10;
        }
        // 添加空值检查，防止空指针异常
        List<FileMsg> fileList = result.getData();
        if(fileList == null) {
        	fileList = new ArrayList<>();
        }
        totalpage = (fileList.size()-1)/num+1;
        Result<List<FileMsg>> page_result = fileservice.showFileByPages(page,num,totalpage);
        
        // 将集合存储到session中
        HttpSession session = request.getSession();
        session.setAttribute("page", page);
        session.setAttribute("num", num);
        session.setAttribute("totalpage", totalpage);
        
        // 确保fileList不为null
        List<FileMsg> pageData = page_result.getData();
        session.setAttribute("fileList", pageData != null ? pageData : new ArrayList<>());
        
        // 重定向到show.jsp
        response.sendRedirect("show.jsp");
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
